package com.treehouse.rpgdefender.core;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;

import com.artemis.Entity;
import com.artemis.World;
import com.treehouse.rpgdefender.component.Body;
import com.treehouse.rpgdefender.component.Select;
import com.treehouse.rpgdefender.component.Sprite;
import com.treehouse.rpgdefender.component.Transformation;
import com.treehouse.rpgdefender.system.BodyRenderSystem;
import com.treehouse.rpgdefender.system.BodySelectSystem;
import com.treehouse.rpgdefender.system.SpriteRenderSystem;

public class Engine extends BasicGame {

	private World world;

	private SpriteRenderSystem spriteRenderSystem;
	private BodyRenderSystem bodyRenderSystem;

	public Engine() {
		super("Engine Test v0.1");
	}

	@Override
	public void init(GameContainer container) throws SlickException {
		this.world = new World();

		this.spriteRenderSystem = new SpriteRenderSystem(container);
		this.bodyRenderSystem = new BodyRenderSystem(container);
		this.world.setSystem(this.spriteRenderSystem, true);
		this.world.setSystem(this.bodyRenderSystem, true);

		this.world.setSystem(new BodySelectSystem(container));

		this.world.initialize();

		Entity test = this.world.createEntity();
		test.addComponent(new Transformation(container.getWidth() / 2,
				container.getHeight() / 2, 0.0f));
		test.addComponent(new Sprite(new Image("resource/images/unknown.png")));
		test.addComponent(new Body(new Rectangle(0, 0, 32, 32)));
		test.addComponent(new Select());
		this.world.addEntity(test);

		Entity test2 = this.world.createEntity();
		test2.addComponent(new Transformation(200, 100));
		test2.addComponent(new Sprite(new Image("resource/images/unknown.png")));
		test2.addComponent(new Body(new Rectangle(0, 0, 64, 64)));
		test2.addComponent(new Select());
		this.world.addEntity(test2);

	}

	@Override
	public void update(GameContainer container, int delta)
			throws SlickException {
		this.world.setDelta(delta);
		this.world.process();
	}

	@Override
	public void render(GameContainer container, Graphics g)
			throws SlickException {
		this.spriteRenderSystem.process();
		this.bodyRenderSystem.process();
	}

	public static void main(String[] args) {
		try {
			AppGameContainer gameContainer = new AppGameContainer(new Engine());
			gameContainer.setDisplayMode(800, 600, false);
			gameContainer.setAlwaysRender(false);
			gameContainer.setShowFPS(true);
			gameContainer.setVSync(true);
			gameContainer.start();
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}
}
